
package com.android.sharecar.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;

/**
 * This activity show tow tabs, they are ReservePickRequestListActivity and
 * ReserveProvideRequestListActivity.
 * 
 * @author xinyan
 * @date 2011-10-13
 */
public class ReserveRequestListTabWidgetActivity extends TabActivity {
    public static final String TAG = "ReserveRequestListTabWidgetActivity";

    // Views
    private ImageButton mHome;

    // Gesture detector that used to switch activity by gesture.
    private GestureDetector mGestureDetector;

    private static int mVerticalMinDistance = 70;

    private static int mMinVelocity = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.tabhost_reservationlist);

        if (customTitle) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.titilebar_reservation_list);
        }
        mHome = (ImageButton) findViewById(R.id.reserveRequestList_ib_home);
        mHome.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MainTabWidgetActivity.class);
                intent.putExtra(MainTabWidgetActivity.CURRENT_TAB,
                        MainTabWidgetActivity.CURRENT_TAB_NORMAL);
                startActivity(intent);
            }
        });

        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ReservePickRequestListActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("ReservePickRequestList").setIndicator("搭车预约").setContent(intent);
        tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ReserveProvideRequestListActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("ReserveProvideRequestList").setIndicator("开车预约")
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

        mGestureDetector = new GestureDetector(this, new OnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (e1.getX() - e2.getX() > mVerticalMinDistance
                        && Math.abs(velocityX) > mMinVelocity) {

                    // 向左手势
                    Intent intent = new Intent(ReserveRequestListTabWidgetActivity.this,
                            MainTabWidgetActivity.class);
                    intent.putExtra(MainTabWidgetActivity.CURRENT_TAB,
                            MainTabWidgetActivity.CURRENT_TAB_TALK);

                    startActivity(intent);
                } else if (e2.getX() - e1.getX() > mVerticalMinDistance
                        && Math.abs(velocityX) > mMinVelocity) {

                    // 向右手势
                    Intent intent = new Intent(ReserveRequestListTabWidgetActivity.this,
                            PickRideRequestListActivity.class);
                    startActivity(intent);
                }
                return false;
            }

        });
        LinearLayout rootLayout = (LinearLayout) findViewById(R.id.tabhost_reservationlist_rootLayout);
        rootLayout.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (dispatchTouchEvent(event)) {
                    return true;
                } else {
                    return false;
                }

            }

        });
        rootLayout.setLongClickable(true);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }
}
